package com.wny.server;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.lang.Math;
import java.lang.Thread;

// A client socket to test the functions of the server 
public class SocketClientTest {
	private static final int serverPort = 8888; // port number defined in the server socket

	public static void main(String[] args) throws IOException  {
		InetAddress addr = InetAddress.getByName("localhost");
		System.out.println("Client addr = " + addr);
		Socket socket = new Socket(addr, serverPort);
		try {
			System.out.println("Client socket = " + socket);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					socket.getInputStream())); // text from input stream
			
			PrintWriter out = new PrintWriter(new BufferedWriter(
					new OutputStreamWriter(socket.getOutputStream())), true); //text for output stream

			// test create account function, a new account includes data of name, pwd, email and sex
			// the header is add=user to tell the server the operation to do is to add user
			// pairs of field name and field value are sent
			// different pairs are seperated by &
			/*out.println("get=user&eventList.eid=4&uid=4"); //request sent from client to server
			String message = in.readLine(); //read response from server
			System.out.println(message);// print out the response sent from the server
			*/
			
			// test create event function, a new event includes data of title, datatiem, description, latitude and longitue
			// some fields can be of NULL value
			// header is add=event
			String message;
			//for(int i = 19; i <= 19; i++) {
			/*	for(int j = 1; j <= 32; j++) {
					boolean OK = (Math.random() > 0.5) ? true : false; 
					if(OK) {
						out.println("add=privacy&uid=" + j + "&private=1");
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					else 
						out.println("add=privacy&uid=" + j + "&private=0");
				}
			//}
			//out.println("add=friend&uid1=5&uid2=8");
			message = in.readLine();
		    System.out.println(message);*/
			
		    // test the log in function, requiring data of name and pwd
		    // header is other=log
		    /*String eventName = "Test5";
		    String year = "2011";
		    String month = "12";
		    String day = "3";
		    String Hour = "18";
		    String Minute = "39";
		    String description = "Test5";

		    out.println("add=event&title="+eventName+"&date="+year+"-"+month+"-"+day+" "+Hour+":"+Minute+":0"+"&description="+description+"&latitude=0&longitude=0"+"\n");
		    message = in.readLine();
		    System.out.println(message);*/
			
		    // test the get event function
		    // header is get=event
		    // if a field value is specified, like the title
		    // then only events with the specified title will be returned
			out.println("get=user");
			message = in.readLine();
		    System.out.println(message);
		    
		    // if no specific fields are specified, all events are returned
		  //  out.println("get=event");
		//	message = in.readLine();
		  //  System.out.println(message);
		    
		    // test the get user function
		    // header is get=user
		    // if a field value is specified, like the name
		    // then only users with the specified name will be returned
		 //   out.println("get=user&name=Chris");
		//	message = in.readLine();
		 //   System.out.println(message);
		    // if no specific fields are specified, all users are returned
		//	out.println("get=user");
		//	message = in.readLine();
		//    System.out.println(message);

			//out.println("get=friend name=<name> sex=<sex>");
			//out.println("add=friend name1=<name1> name2=<name2>");
			/*
			out.println("get=user&location=1#0#2"); //get=user&location=<lat>||<long>||<radius>
			String message = in.readLine();
			System.out.println(message);
			
			out.println("get=event&location=1#0#2"); //get=event&location=<lat>||<long>||<radius>
			message = in.readLine();
			System.out.println(message);
			
			out.println("get=user&location=1#0#2&name=Li"); //get=user&location=<lat>||<long>||<radius>&name=<name>
			message = in.readLine();
			System.out.println(message);
			*/
			//out.println("END");// must send server a END message to terminate this communication
		} finally {
			System.out.println("Client closing...");
			socket.close();
		}
	}
}
